//
//  HMIndividualityCell.m
//  BestLearning
//
//  Created by 陈辉明 on 2017/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HMIndividualityCell.h"
#import "HNTeacgerEvaluateDetailModel.h"
#import "HNAudioTapGestureRecognizer.h"
#import "EMCDDeviceManager.h"
#import "HNAudioView.h"

@interface HMIndividualityCell()

@property (weak, nonatomic) IBOutlet UILabel *contenLabel;
@property (weak, nonatomic) IBOutlet UIImageView *personImage;
@property (nonatomic, strong) HNAudioView *AudioView;

@end


@implementation HMIndividualityCell


-(void)setModel:(HNTeacgerEvaluateDetailModel *)model {
    _model = model;
    if ([NSString isBlankString:model.personalProblem]) {
        self.contenLabel.text = @"";
    }else {
        self.contenLabel.text = [NSString stringWithFormat:@"%@", model.personalProblem];
    }
    
    //个性问题图片
    if ([self JudgImgAndAudio:model.wmrPath])
    {
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
        [self.personImage sd_setImageWithURL:[NSURL URLWithString:model.wmrPath] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            Tap.Img = self.personImage.image;
        }];
        [self.personImage addGestureRecognizer:Tap];
        self.personImage.userInteractionEnabled = YES;
    }
    else
    {
        self.personImage.image = [UIImage imageNamed:@"iocn_voice"];
        /** 增加播放手势 */
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
        Tap.UrlStr = model.wmrPath;
        [self.personImage addGestureRecognizer:Tap];
        self.personImage.userInteractionEnabled = YES;
    }
   
}





/** YES是图片,NO是音频 */
- (BOOL)JudgImgAndAudio:(NSString *)Str
{
    if ([Str isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else
    {
        if ([HNTeacgerEvaluateDetailModel JudgeStrIsImagOrAudio:Str])
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
}

#pragma mark - 图片全屏
/** 图片全屏手势 */
- (void)ImgAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self ImgControllerWithImg:Tap.Img];
}
/** 图片全屏操作 */
- (void)ImgControllerWithImg:(UIImage *)Img
{
    [self.AudioView OpenWithImg:Img];
}


#pragma mark - 音频播放
/** 播放手势 */
- (void)PlayerAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self PlayerWithUrlStr:Tap.UrlStr];
}
/** 音频播放 */
- (void)PlayerWithUrlStr:(NSString *)UrlStr
{
    [self RefreshDataForAmr:UrlStr Complete:^(NSString *FullPath) {
        
        FullPath = [FullPath substringFromIndex:7];
        [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:FullPath completion:^(NSError *error) {
            NSLog(@"播放完成");
        }];
    }];
}

/**
 获取amr文件
 */
- (void)RefreshDataForAmr:(NSString *)UrlStr Complete:(void(^)(NSString *FullPath))Complete
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"AMR"];
    
    [NetManager DownLoadFileWithUrlStr:UrlStr FileName:[UrlStr lastPathComponent] Path:path Progress:^(CGFloat Index) {
        
        
    } Complete:^(NSString *FullPath, NSError *Err) {
        
        [MBProgressHUD hideHUD];
        if (Err)
        {
            [MBProgressHUD showError:@"音频文件播放失败"];
        }
        else
        {
            Complete (FullPath);
        }
    }];
}

- (HNAudioView *)AudioView
{
    if (!_AudioView)
    {
        _AudioView = [[HNAudioView alloc] init];
    }
    return _AudioView;
}




@end
